<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>e网批发管理后台</title>
  <?php 
include("common/conn.php"); if($_SESSION['userid']==""){echo "<script>window.location.href='login.php';</script>";} 
// 获取当前时间
date_default_timezone_set('Asia/Shanghai');
$time = date("Y-m-d H:i:s");
// 获取当前时间 end
  if($_GET['del']=="ok")
  {
    $area = array(); 
    $area= $_POST['checkbox'];

    foreach ($area as $k=>$v)
    {
      $srat1=mysql_query("DELETE FROM goodsorders where ordersid='".$v."'");
      $srat1=mysql_query("DELETE FROM car where ordersid='".$v."'");
    }

  }

  if(isset($_POST['kuaidi']))
  {
    $state=2;
    $srat=mysql_query("update goodsorders set kuaidi='".$_POST[kuaidi]."',danhao='".$_POST[danhao]."',state='".$state."' where ordersid='".$_POST['ordersid']."'");

    // 调用信息推送接口
    $url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$_POST[ordersid];
    file_get_contents($url);
    // 调用信息推送接口 end
    // 查找订单自动确认收货时间 
    $query = mysql_query("select confirm_time from categroy_type where id=(select categroy_type_id from categroy where cateid=(select leimu from shop where shopid=(select shopid from goodsorders where ordersid=".$_POST[ordersid].")))");
    $categroy_type = mysql_fetch_assoc($query);
    $confirm_time = $categroy_type[confirm_time];
    // 查找订单自动确认收货时间 end
    // 定时器
    $url = "http://120.24.0.10/xzpf/changeAfterTime/changeOrderState.php?ordersid=".$_POST[ordersid]."";
    $start_date = date("Y-m-d",strtotime(''.$confirm_time.'days'));
    $start_hour = date("H");
    $start_minute = date("i");
    $tag = 'setOrderState';
    $loop = 1;

    $apiUrl = "http://120.24.0.10/unitask/core/func.add.php?url=".$url."&start_date=".$start_date."&start_hour=".$start_hour."&start_minute=".$start_minute."&tag=".$tag."&loop=".$loop."";
    file_get_contents($apiUrl);
    // 定时器 end
    echo "<script>window.location.href='index.php?state=".$state."'</script>";
  }

// 查找快递公司
  $query=mysql_query("select * from express_delivery");
  while ($row=mysql_fetch_assoc($query)) {
    $express[]=$row;
  }
// 查找快递公司 end
// 平台审核
if ($_REQUEST[act]=='checkpass') {
  $query = mysql_query("UPDATE tuihuo SET state=4, `3time`='".$time."' where ordersid=".$_REQUEST[ordersid]);
  echo "<script>window.location.href='index.php?state=4'</script>";
}elseif ($_REQUEST[act]=='checkfalse') {
  $query = mysql_query("UPDATE tuihuo SET state=5, `3time`='".$time."' where ordersid=".$_REQUEST[ordersid]);
  echo "<script>window.location.href='index.php?state=4'</script>";
}
// 平台审核 end
// 平台放款
if ($_REQUEST[act]=='payback') {
  // 更改退货表状态
  $query = mysql_query("UPDATE tuihuo set state=6, `4time`='".$time."'  where ordersid=".$_REQUEST[ordersid]);
  // 更改退货表状态 end
  
  // 根据单号查找订单信息
  $query = mysql_query("select * from goodsorders where ordersid='".$_REQUEST[ordersid]."'");
  $goodsorders = mysql_fetch_assoc($query);
  // 根据单号查找订单信息 end
  $query = mysql_query("select name from shop where shopid=".$goodsorders[shopid]."");
  $shop = mysql_fetch_assoc($query);
  // 记录消费者购买商品时商家的现金流量记录
  $content="消费者退货成功,商家".$shop[name]."销售收入减少".$goodsorders[totalprice]."元";
  $query = mysql_query("INSERT INTO cash_flow (amount,shopid,content) value (".-$goodsorders[totalprice].",".$goodsorders[shopid].",'".$content."')");
  // 记录消费者购买商品时商家的现金流量记录 end
  // 记录消费者购买商品时滞留在平台的现金流量记录
  $shopid=0; // shopid=0表示平台
  $content="消费者退货成功,商家".$shop[name]."滞留在平台的资金减少".$goodsorders[totalprice]."元";
  $query = mysql_query("INSERT INTO cash_flow (amount,shopid,fromshopid,content) value (".-$goodsorders[totalprice].",".$shopid.",".$goodsorders[shopid].",'".$content."')");
  // 记录消费者购买商品时滞留在平台的现金流量记录 end
  // 记录到现金流量表 end
  // 更新平台的资金
  $query = mysql_query("UPDATE admin set property=property-".$goodsorders[totalprice]);
  // 更新平台的资金 end
  // 更新商家的余额
  $query = mysql_query("UPDATE shop set balance=balance-".$goodsorders[totalprice]." where shopid=".$goodsorders[shopid]."");
  // 更新商家的余额 end
  echo "<script>window.location.href='index.php?state=4'</script>";
}
// 平台放款 end

// 关闭订单 
if ($_REQUEST['act'] == 'close') {
  $query = mysql_query("select * from tuihuo where ordersid=".$_REQUEST['ordersid']);
  $tuihuo = mysql_fetch_assoc($query);
  if ($tuihuo) {
    $query = mysql_query("DELETE FROM tuihuo where ordersid=".$_REQUEST['ordersid']);
  }
  $query = mysql_query("UPDATE goodsorders set state=6 where ordersid=".$_REQUEST['ordersid']);
  if (mysql_affected_rows()>0) {
    echo "<script>alert('订单关闭成功');window.location.href='index.php?state=6'</script>";
  }else{
    echo "<script>alert('订单关闭失败');window.location.href='index.php?state=6'</script>";
  }
}
// 关闭订单 end

// 取消退货 
if ($_REQUEST['act'] == 'cancel') {
  $query = mysql_query("select orderstate from tuihuo where ordersid=".$_REQUEST['ordersid']);
  $tuihuo = mysql_fetch_assoc($query);
  $orderstate = $tuihuo['orderstate'];

  $query = mysql_query("DELETE FROM tuihuo where ordersid=".$_REQUEST['ordersid']);
  $query = mysql_query("UPDATE goodsorders set state='".$orderstate."' where ordersid=".$_REQUEST['ordersid']);
  if (mysql_affected_rows()>0) {
    echo "<script>alert('取消退货成功');window.location.href='index.php?state=".$orderstate."'</script>";
  }else{
    echo "<script>alert('取消退货失败');window.location.href='index.php?state=".$orderstate."'</script>";
  }
}
// 取消退货 end
  ?>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <script type="text/JavaScript">
    <!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>

  <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->


  <!-- staqrt subnav -->
  <div id="sub_nav">

    <div id="subnav_wrap" class="container_12">

      <!-- start sub nav list -->
      <div id="subnav" class=" grid_12">

        <ul>

          <li><a href="index.php" <?php if($_GET['state']==""){echo 'class="sub_nav_active"';}?>>全部</a></li>
          <li><a href="index.php?state=0" <?php if($_GET['state']=="0"){echo 'class="sub_nav_active"';}?>>待付款</a></li>
          <!-- <li><a href="index.php?state=1" <?php if($_GET['state']=="1"){echo 'class="sub_nav_active"';}?>>待商家接单</a></li> -->
          <li><a href="index.php?state=8" <?php if($_GET['state']=="8"){echo 'class="sub_nav_active"';}?>>待发货</a></li>
          <!-- <li><a href="index.php?state=9" <?php if($_GET['state']=="9"){echo 'class="sub_nav_active"';}?>>商家拒绝接单</a></li> -->
          <li><a href="index.php?state=2" <?php if($_GET['state']=="2"){echo 'class="sub_nav_active"';}?>>待收货</a></li>
          <li><a href="index.php?state=3" <?php if($_GET['state']=="3"){echo 'class="sub_nav_active"';}?>>待评价</a></li> 
          <li><a href="index.php?state=4" <?php if($_GET['state']=="4"){echo 'class="sub_nav_active"';}?>>售后</a></li>  
          <!-- <li><a href="index.php?state=5" <?php if($_GET['state']=="5"){echo 'class="sub_nav_active"';}?>>交易完成</a></li>     -->
          <!-- <li><a href="index.php?state=6" <?php if($_GET['state']=="6"){echo 'class="sub_nav_active"';}?>>交易关闭</a></li>     -->
        </ul>

      </div>
      <!-- end subnavigation list -->	

    </div>
  </div>
  <div class="container_12">
    <form action="?id=<?php echo $_REQUEST['id']; ?>" method="post">
      <label>市场</label>
      <select name="classify" class="select" onchange="MM_jumpMenu('parent',this,0)">
        <?php if (!isset($_GET[cityid]) ) {?>
        <option value="?path=<?php echo $_GET[path]; ?>">全部市场</option>
        <?php }?>
        <?php if($_GET[cityid]!=""){?>
        <option value="?path=<?php echo $_GET[path]; ?>">全部市场</option>
        <?php }else{?>
        <?php } $rert32=mysql_query("select * from city order by id desc ");
        while($sdg32=mysql_fetch_array($rert32)){
          $query=mysql_query("select * from shop where shopid='".$_GET[shopid]."'");
          $result=mysql_fetch_array($query);
          ?>                  
          <option  value="?city=<?php echo $sdg32[city];?>&cityid=<?php echo $sdg32[id];?>" <?php if ($sdg32[id]==$_REQUEST[cityid]) { ?>selected<?php } ?>><?php echo $sdg32[city];?></option>
          <?php }?>

        </select>

        <label>商家</label>
        <select name="classify" class="select" onchange="MM_jumpMenu('parent',this,0)">
          <?php if (!isset($_GET[shopid])) {?>
          <option value="?path=<?php echo $_GET[path]; ?>&cityid=<?php echo $_REQUEST[cityid]; ?>">全部商家</option>
          <?php }?>
          <?php if($_GET[shop]!=""){?>
          <option value="?path=<?php echo $_GET[path]; ?>&cityid=<?php echo $_REQUEST[cityid]; ?>">全部商家</option>
          <?php echo $_GET[shop];?></option>
          <?php }else{?>      			
          <?php }	
          $_sql = "select * from shop ";
          if($_GET[cityid]!=""){
            $_sql.=" where city=$_GET[cityid]";
          }
          $_sql.=" order by shopid desc ";
          $rert32=mysql_query($_sql);
          while($sdg32=mysql_fetch_array($rert32)){?>
          <option <?php if ($sdg32['shopid']==$_REQUEST['shopid']) { ?>selected<?php } ?> value="?cityid=<?php echo $_REQUEST[cityid]; ?>&shopid=<?php echo $sdg32['shopid'];?>&shop=<?php echo $sdg32['name'];?>"><?php echo $sdg32['name'];?></option>
          <?php }?>

        </select>
        <label>订单号：</label>
        <input type="text" name="orderid" value="111111">
        <input type="submit" name="submit" value="搜索">
      </form>
    </div>

  </div>
  <!-- end sub_nav -->
  <div class="container_12">
    <form action="?del=ok" method="post" id="form">
      <!-- EVERYTING BELOW IS THE MAIN CONTENT -->

      <?php 
      $state=3;
$perNumber=10; //每页显示的记录数
$page=$_GET['page']; //获得当前的页面值

if($_GET[state]!="")
{
  $count=mysql_query("select count(*) from goodsorders where  state ='".$_GET[state]."'");
}else if($_REQUEST['shopid']!=""){
  $count=mysql_query("select count(*) from goodsorders where shopid = '".$_REQUEST[shopid]."'");


}

else if($_REQUEST['cityid']!=""){

  $count=mysql_query("select count(*) from goodsorders where shopid in (select shopid from shop where city='".$_REQUEST[cityid]."')");

}else
{
  $count=mysql_query("select count(*) from goodsorders ");
}

//获得记录总数
$rs=mysql_fetch_array($count); 
$totalNumber=$rs[0];
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数

if (!isset($page)) {
  $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的
if($_GET[state]!="")
{
  $rert=mysql_query("select * from goodsorders where  state ='".$_GET[state]."' order by ordersid desc limit $startCount,$perNumber"); 
}	
else if($_REQUEST[shopid]!=""){


  $rert=mysql_query("select * from goodsorders where  shopid = '".$_REQUEST[shopid]."' order by ordersid desc limit $startCount,$perNumber");
}

else if($_REQUEST[cityid]!=""){

  $rert=mysql_query("select * from goodsorders where shopid in (select shopid from shop where city='".$_REQUEST[cityid]."')  order by ordersid desc limit $startCount,$perNumber");

}

else
{
  $rert=mysql_query("select * from goodsorders order by ordersid desc limit $startCount,$perNumber"); 
}
while($sdg3=mysql_fetch_array($rert))
{	

  $count2=mysql_query("select count(*) from car where  ordersid ='".$sdg3[ordersid]."'");

//获得订单记录总数
  $rs2=mysql_fetch_array($count2);
  $totalNumber2=$rs2[0];
  ?>
  <div class="cy_mode" style="background: #fff; border:0px;">
    <input type="checkbox" name="checkbox[]" id="checkbox"  value="<?php echo $sdg3['ordersid'];?>"/>  <strong>订单号：<?php echo $sdg3[numbers];?></strong>
    <a href="file2.php?id=<?php echo $sdg3['ordersid']; ?>&state=<?php echo $_REQUEST['state']; ?>" ><div class="dr_in">详情</div></a>
    <?php if($sdg3['state']=="1" || $sdg3['state']=="7"){?>
    <div class="dr_in2" >等待商家接单</div>
    <?php }else if($sdg3['state']=="0"){?>
    <div class="dr_in2">等待客户付款</div>
    <?php }else if($sdg3['state']=="3"){?>
    <div class="dr_in2">等待客户评价</div>

    <?php }else if($sdg3['state']=="8"){?>
    <div class="dr_in2">待商家发货</div>

    <?php }else if($sdg3['state']=="9"){?>
    <div class="dr_in close">关闭订单</div>

    <?php }else if($sdg3['state']=="4"){?>
    <?php
      $query = mysql_query("select * from tuihuo where ordersid=".$sdg3['ordersid']);
      $tuihuo = mysql_fetch_assoc($query);
      // 海淘
      if ($sdg3['type'] == 0) {
        if ($tuihuo[state]==1) { ?>
        <div class="dr_in2">待商家审核</div>

        <?php }elseif ($tuihuo[state]==2) { ?>

          <?php if ($tuihuo[orderstate]==2 || $tuihuo[orderstate]==3) { ?>
            <div class="dr_in2">待用户退货</div>
          <?php }else{ ?>
            <div class="dr_in close">关闭订单</div>
          <?php } ?>

        <?php }elseif ($tuihuo[state]==3) { ?>
        <div class="dr_in2">商家拒绝退货</div>

        <?php }elseif ($tuihuo[state]==4) { ?>
         <div class="dr_in2">待商家回收</div>
        <?php }elseif ($tuihuo[state]==5) { ?>
        <div class="dr_in close">关闭订单</div>
        <?php }elseif ($tuihuo[state]==6) { ?>
        <div class="dr_in2">退货完成</div>
        <?php } 
      // 快购
      }elseif ($sdg3['type'] == 1) {
        if ($tuihuo[state]==1) { ?>
        <div class="dr_in cancel">取消退货</div>
        <div class="dr_in close">关闭订单</div>
        <?php } 
      }
?>
    <?php }else if($sdg3['state']=="5"){?>
    <div class="dr_in2">交易完成</div>
    <?php }else if($sdg3['state']=="6"){?>
    <div class="dr_in2">交易关闭</div>
    <?php }else{?>
    <div class="dr_in2">待客户收货</div>
    <?php }?>

  </div>
  <?php 
  $state = 1;
  $rert2 = mysql_query("select * from car where  ordersid ='".$sdg3['ordersid']."' order by carid desc"); 
  while($sdg2=mysql_fetch_array($rert2))

  {


    $rert4=mysql_query("select * from goods where  goodsid ='".$sdg2['goodsid']."'"); 
    $sdg=mysql_fetch_array($rert4);

    $ime=$sdg['images'];
    $de = array();
    $de=explode("," ,$ime);
    $images=$de[0];

    ?>
    <div class="cy_mode" style="background:#FFFFFF; margin-top:0px;">
      <div class="dr_mimg">
        <img src="<?php echo $images; ?>" width="50%"  height="auto">
      </div>
      <div class="dr_conm">

        <strong><?php echo $sdg[title]; ?></strong><span class="conf_wd all_fr">￥<?php echo $sdg2[price]; ?></span>
        <br/>
        <span class="conf_wd">规格：<?php echo $sdg2[fTitle].$sdg2[sTitle]; ?></span><span class="conf_wd all_fr">x<?php echo $sdg2[number]; ?></span>
        <br/>
        <span class="conf_wd all_fr">总计：￥<?php echo ($sdg2[price]*$sdg2[number]); ?></span>
        总计：<?php echo $totalNumber2; ?>件产品</span>
      </div>

    </div>


    <?php }}?>
</form>
    <div class="grid_12">

      <div class="clearfix"><input  type="button"  value="删除选中"  class="del submit" /></div>

      <ul id="pagination">



        <?php  

        if ($page > 1) { //上一页 ?>
        <li><a     href="?page=1&state=<?php echo $_REQUEST[state]; ?>">首页</a></li>
        <li><a     href="?page=<?php echo ($page-1); ?>&state=<?php echo $_REQUEST[state]; ?>">上一页</a></li>
        <?php }?>

        <li class="active"><?php echo $page;?></li>

        <?php



        if ($_GET[page]!=$totalPage) { //下一页 ?>
        <li><a href="?page=<?php echo ($page+1); ?>&state=<?php echo $_REQUEST[state]; ?>">下一页</a></li>
        <?php }

        if ($totalPage>1) { //下一页 ?>
        <li><a href="?page=<?php echo $totalPage; ?>&state=<?php echo $_REQUEST[state]; ?>">尾页</a></li>

        <?php }?>

        <input type="text" class="text skip_val" style="width: 20px;height:12px;margin:0 5px;"><button class="submit skip">跳转</button>
        总共有<?php echo $totalNumber;?>个订单 
      </ul>

    </div>           







  </div>
  <!-- end content wrap -->
</div>
<!-- 遮罩层 -->
<div class="mask_div">
</div>
<!-- 遮罩层 end -->
<div class="alert_div">
  <div class="alert_title">
    填写快递单号
  </div> 
  <div class="alert_line"></div>
  <div class="alert_body">
    <form id="alert_form" action="" method="post">
      <label>快递公司:</label>
      <select name="kuaidi" class="select">
        <?php foreach ($express as $v) { ?>
        <option value="<?php echo $v[id]; ?>"><?php echo $v[name] ?></option>
        <?php } ?>
      </select>
      <label>快递单号:</label><input type="text" class="text" name="danhao" value="">
      <input type="hidden" name="ordersid" id="ordersid" value="">
    </form>
  </div>
  <div class="alert_foot">
    <div class="foot-div">
      <button type="button" class="submit" id="submit1">提交</button>
      <button type="button" class="submit" id="cancel1">取消</button>
    </div>
  </div>

</div>
<!-- 审核不通过时平台说明 -->
<!-- <div class="platform_comment_div">
  <div class="platform_comment">
    <div class="platform_comment_title">
      审核不通过时平台说明
    </div>
    <div class="platform_comment_body">
      <textarea></textarea>
    </div>   
  </div>
</div> -->
<!-- 审核不通过时平台说明 end -->
</body>
<script type="text/javascript">
  $('.send').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    $('#ordersid').attr('value',ordersid);
    $('.mask_div').show();
    $('.alert_div input[type="text"]').attr('value','');
    $('.alert_div').show();
  })
  $('#submit1').click(function(){
    $('#alert_form').submit();
    $('.mask_div').hide();
    $('.alert_div').hide();
  });
  $('#cancel1').click(function(){
    $('.mask_div').hide();
    $('.alert_div').hide();
  })

  // 关闭订单 
  $('.close').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    window.location.href='index.php?act=close&ordersid='+ordersid;
  })
  // 关闭订单 end

  // 取消退货 
  $('.cancel').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    window.location.href='index.php?act=cancel&ordersid='+ordersid;
  })
  // 取消退货 end

  // 删除 
  $('.del').click(function(){
    $('#form').submit();
  })
  // 删除 end

  // 页面跳转 
  $('.skip').click(function(){
    var page = $('.skip_val').val();
    var url = "index.php?page="+page;
    window.location.href = url;
  })
  // 页面跳转 end
</script>
</html>
